//
//  AddClientViewController.m
//  quickBatchManager
//
//  Created by jack on 15/9/24.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "AddClientViewController.h"
#import "ClientModel.h"
@interface AddClientViewController ()
{
    NSMutableArray *buttonList;
    NSArray *listsize;
}
@end

@implementation AddClientViewController
@synthesize bgView,exitBtn,addBtn,comfirmBtn,mTextField;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)initUI
{
    self.view.backgroundColor=[UIColor clearColor];
    UIImage *image = bgView.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    bgView.image=image;
    
    [exitBtn bk_whenTapped:^{
         [self dismissViewControllerAnimated:YES completion:nil];
        
        }];
    
    [addBtn bk_whenTapped:^{
        if(![mTextField.text isBlank])
        {
            ClientModel *client=[[ClientModel alloc] init];
            client.clientName=mTextField.text;
            client.clientType=_clientTypeBtn.selected;
            [ClientModel insertToDB:client];
             mTextField.text=@"";
            [self reloadData];
            
        }
        else [self showAlert:@"请输入"];
    }];
    
    [_clientTypeBtn bk_whenTapped:^{
        _clientTypeBtn.selected=(!_clientTypeBtn.selected);
         [self reloadData];
    }];
    
    [comfirmBtn bk_whenTapped:^{
        for (UIButton *btn in buttonList) {
           if(btn.selected)
           {
               if([_delegate respondsToSelector:@selector(didSelectData:andData:)])
               {
                   [_delegate didSelectData:ChooseTypeClient andData:listsize[btn.tag]];
               }
               [self dismissViewControllerAnimated:YES completion:nil];
           }
        }
    }];
    
    
    [self reloadData];
}

-(void) initData
{
    buttonList=[[NSMutableArray alloc] init];
}

-(void) reloadData
{
    for (UIView *view in _listScrollView.subviews) {
        [view removeFromSuperview];
    }
    [buttonList removeAllObjects];
    
    int scrollwidth=405;
    
    listsize=[ClientModel searchWithWhere:@{@"clientType":[ToolUtils numberToStr:_clientTypeBtn.isSelected?1:0]}];
    int i=0;
    for (ClientModel *client in listsize) {
        UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake((scrollwidth/3)*(i%3)+10, (i/3)*45, scrollwidth/3-20, 35)];
        [button setTitle:client.clientName forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:0.68 green:0.40 blue:0.22 alpha:1] forState:UIControlStateNormal];
        [button setBackgroundImage:[ToolUtils imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[ToolUtils imageWithColor:[UIColor colorWithRed:1 green:0.84 blue:0.66 alpha:1]] forState:UIControlStateSelected];
        button.tag=i;
        
        button.layer.cornerRadius=5;
        button.clipsToBounds=YES;
        button.titleLabel.font=[UIFont systemFontOfSize:15];
        [button bk_whenTapped:^{
            for (UIButton *btn in buttonList) {
                btn.selected = NO;
            }
            button.selected=YES;
            
        }];
        [_listScrollView addSubview:button];
        [buttonList addObject:button];
        i++;
    }
    
    _listScrollView.contentSize=CGSizeMake(scrollwidth, (i/3+1)*45);
    
}


@end
